package com.boost.socialNetwork.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

public class Registration extends LayoutPanel {

	private Label head;
	private TextBox firstName;
	private TextBox lastName;
	private TextBox username;
	private ListBox country;
	private CheckBox agree;
	private Label firstnameReq;
	private Label usernameReq;
	private Label lblMustMatch;
	private PasswordTextBox password;
	private PasswordTextBox confirm;

	private final UserServiceAsync regService = GWT
			.create(UserService.class);
	private TextBox EmailBox;
	private Label Email_validation;

	public Registration() {
		//HistoryManager.newHistory("PageReg");
		
		head = new Label("Registration");
		FlexTable flexTable = new FlexTable();
		add(flexTable);
		setWidgetLeftWidth(flexTable, 67.0, Unit.PX, 390.0, Unit.PX);
		setWidgetTopHeight(flexTable, 10.0, Unit.PX, 298.0, Unit.PX);

		Label lblFirstName = new Label("First Name:");
		lblFirstName.setStyleName("gwt-regLabel");
		flexTable.setWidget(0, 0, lblFirstName);

		firstName = new TextBox();
		flexTable.setWidget(0, 1, firstName);

		firstnameReq = new Label("field required");
		firstnameReq.setStyleName("gwt-validation");
		flexTable.setWidget(0, 2, firstnameReq);
		firstnameReq.setVisible(false);

		Label lblLastName = new Label("Last Name:");
		lblLastName.setStyleName("gwt-regLabel");
		flexTable.setWidget(1, 0, lblLastName);

		lastName = new TextBox();
		flexTable.setWidget(1, 1, lastName);

		Label lblEmail = new Label("Email:");
		lblEmail.setStyleName("gwt-regLabel");
		flexTable.setWidget(2, 0, lblEmail);

		EmailBox = new TextBox();
		flexTable.setWidget(2, 1, EmailBox);

		Email_validation = new Label("field required");
		Email_validation.setVisible(false);
		Email_validation.setWordWrap(false);
		Email_validation.setStyleName("gwt-validation");
		flexTable.setWidget(2, 2, Email_validation);

		Label lblUsername = new Label("Username:");
		lblUsername.setStyleName("gwt-regLabel");
		flexTable.setWidget(3, 0, lblUsername);

		username = new TextBox();
		flexTable.setWidget(3, 1, username);

		usernameReq = new Label("field required");
		usernameReq.setStyleName("gwt-validation");
		flexTable.setWidget(3, 2, usernameReq);
		usernameReq.setVisible(false);

		Label lblPassword = new Label("Password: ");
		lblPassword.setStyleName("gwt-regLabel");
		flexTable.setWidget(4, 0, lblPassword);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1,
				HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_TOP);

		password = new PasswordTextBox();
		flexTable.setWidget(4, 1, password);

		Label lblConfirmPassword = new Label("Confirm Password:       ");
		lblConfirmPassword.setStyleName("gwt-regLabel");
		flexTable.setWidget(5, 0, lblConfirmPassword);

		confirm = new PasswordTextBox();
		confirm.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if (!password.getText().equals(confirm.getText())) {
					password.setStyleName("gwt-red");
					confirm.setStyleName("gwt-red");
				} else {
					password.setStyleName("gwt-green");
					confirm.setStyleName("gwt-green");
				}
			}
		});
		confirm.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				if (!password.getText().equals(confirm.getText())) {
					password.setStyleName("gwt-red");
					confirm.setStyleName("gwt-red");
				} else {
					password.setStyleName("gwt-green");
					confirm.setStyleName("gwt-green");
				}
			}
		});
		flexTable.setWidget(5, 1, confirm);

		lblMustMatch = new Label("must match");
		lblMustMatch.setStyleName("gwt-validation");
		flexTable.setWidget(5, 2, lblMustMatch);
		lblMustMatch.setVisible(false);

		Label lblCountry = new Label("Country: ");
		lblCountry.setStyleName("gwt-regLabel");
		flexTable.setWidget(6, 0, lblCountry);

		country = new ListBox();
		country.addItem("");
		country.addItem("Australia");
		country.addItem("Brazil");
		country.addItem("Canada");
		country.addItem("Denmark");
		country.addItem("Egypt");
		country.addItem("France");
		country.addItem("Germany");
		country.addItem("Hungry");
		country.addItem("Italy");
		country.addItem("Jamica");
		country.addItem("kazakhstan");
		country.addItem("Latvia");
		country.addItem("Malaysia");
		country.addItem("Norway");
		country.addItem("Oman");
		country.addItem("Poland");
		country.addItem("Queen's Land");
		country.addItem("Russia");
		country.addItem("Saudi Arabia");
		country.addItem("Tailand");
		country.addItem("United States");
		country.addItem("Veitman");
		country.addItem("Wales");
		country.addItem("Yemen");
		country.addItem("Zambia");
		flexTable.setWidget(6, 1, country);
		country.setSize("171px", "32px");

		agree = new CheckBox("I agree to registration terms");
		add(agree);
		setWidgetLeftWidth(agree, 158.0, Unit.PX, 199.0, Unit.PX);
		setWidgetTopHeight(agree, 281.0, Unit.PX, 19.0, Unit.PX);

		Button btnRegister = new Button("Register");
		btnRegister.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (firstName.getText().equals("")) {
					firstnameReq.setVisible(true);
				}
				if (!EmailBox.getText().matches(
						"^\\w+[\\w-\\.]*\\@\\w+((-\\w+)|(\\w*))\\.[a-z]{2,3}$")) {
					Email_validation.setVisible(true);

				}
				if (!firstName.getText().equals("")) {
					firstnameReq.setVisible(false);
				}
				if (username.getText().equals("")) {
					usernameReq.setVisible(true);
				}
				if (!password.getText().equals(confirm.getText())) {
					lblMustMatch.setVisible(true);
				}
				if (password.getText().equals(confirm.getText())) {
					lblMustMatch.setVisible(false);
				}
				if (!username.getText().equals("")) {
					usernameReq.setVisible(false);
				}

				if (!agree.isChecked()) {
					Window.alert("You have to agree to the registration terms!!");
				}
				if (agree.isChecked()
						&& !firstName.getText().equals("")
						&& !username.getText().equals("")
						&& EmailBox
								.getText()
								.matches(
										"^\\w+[\\w-\\.]*\\@\\w+((-\\w+)|(\\w*))\\.[a-z]{2,3}$")) {
					regService.register(username.getText(),
							password.getText(), firstName.getText(),
							lastName.getText(),
							country.getItemText(country.getSelectedIndex()),
							EmailBox.getText(), new AsyncCallback<String>() {

								@Override
								public void onSuccess(String result) {
									if (result.equals("OK")) {
								//		RootPanel.get("post").clear();
								//		RootPanel.get("head").clear();
								//		Login login = new Login();
								//		RootPanel.get("post").add(login);
								//		RootPanel.get("head").add(
								//				login.getHead());
									HistoryManager.newHistory("PageLogin");
										Window.alert("Your account has been registered, You will be directed to the login page");
									} else {
										if (result.equals("Already exisits")) {
											Window.alert("username already exists");
										} else {
											Window.alert("Datastore closed");
										}
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub

								}
							});
				}
			}
		});
		add(btnRegister);
		setWidgetLeftWidth(btnRegister, 196.0, Unit.PX, 103.0, Unit.PX);
		setWidgetTopHeight(btnRegister, 306.0, Unit.PX, 30.0, Unit.PX);
/*		HistoryManager.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {

				HistoryManager.HistoryChange(event);
			}
		});*/
	}

	public Label getHead() {
		return head;
	}
}
